# -*-coding:utf-8 -*-
import socket
import logging
logging.basicConfig(level=logging.DEBUG,  
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',  
                    datefmt='%a, %d %b %Y %H:%M:%S',  
                    filename='/tmp/test.log',  
                    filemode='w')

def cmdSend(aCmd):
    try:
        s = socket.socket()
        s.connect(('192.168.1.218',6900))
        s.sendall(aCmd)
        recv = s.recv(1024)
        s.close()
        return recv
    except socket.gaierror, e:
        print e
    except socket.error, e:
        print e
    return ''

def dbSelectDevStatus(aCard,aDid) :
    #print "aDid=",aDid
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_MY_CARD_GETSTATUS('"+ aCard + "','" + aDid+"')</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd).split("|")[1].replace("&v","")
    #tmpRet = cmdSend(tmpCmd)
    print "dbSelectDevStatus=",tmpRet
    return tmpRet

def dbSelectDevStart(aCard,aDid,a_value):
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_MY_CARD_START_WATER('"+aCard+"','"+aDid+"','"+ a_value +"')</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd).split("|")[1].replace("&v","")
    print tmpRet
    return tmpRet

def dbSelectDevStop(aCard,aDid,a_value):
    tmpCmd = "<msg><cmd class='select' conname='' clientid='' istran=''><body>select F_YLW_MY_CARD_STOP_WATER('"+aCard+"','"+aDid+"','"+ a_value +"')</body></cmd></msg>"
    tmpRet = cmdSend(tmpCmd).split("|")[1].replace("&v","")
    print "dbSelectDevStop=", tmpRet
    return tmpRet

if __name__ == '__main__':
    dbSelectDevStatus('24B5B806','1234567890654777')
    #dbSelectDevStart('24B5B806','1234567890654777','1.00')
    #dbSelectDevStop('24B5B806','1234567890654777','2.00')